<template>
  <div class="content">
    <div>
      <el-button @click="openFormDialog(form,'add')">新增菜单</el-button>
      <el-table
          :data="menuTree"
          row-key="id"
          stripe
          :row-style="{height:'40px'}"
          :cell-style="{padding:'0px'}"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            prop="name"
            label="菜单名称">
        </el-table-column>
        <el-table-column
            prop="url"
            label="路由地址">
        </el-table-column>
        <el-table-column
            prop="icon"
            label="菜单图标">
          <template slot-scope="scope">
            <i :class="scope.row.icon"></i>
          </template>
        </el-table-column>
        <el-table-column
            prop="sort"
            label="排序">
        </el-table-column>
        <el-table-column
            prop="parentId"
            label="父ID">
        </el-table-column>
        <el-table-column
            label="操作"
            width="200">
          <template slot-scope="scope">
            <el-button @click="openFormDialog(scope.row,'addSub')" type="text" size="small">新增子菜单</el-button>
            <el-button @click="openFormDialog(scope.row,'edit')" type="text" size="small">编辑</el-button>
            <el-button @click="deleteById(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="修改菜单" :visible.sync="dialogFormVisible" width="600px">
      <el-form :model="form" ref="form">
        <el-form-item label="菜单名称" prop="name" :label-width="formLabelWidth"
                      :rules="[
                                  { required: true, message: '请填写完整'}
                                ]">
          <el-input v-model="form.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="路由地址" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="菜单图标" :label-width="formLabelWidth">
          <el-select v-model="form.icon" placeholder="请选择菜单图标">
            <el-option value="">无</el-option>
            <el-option value="el-icon-setting"><i class="el-icon-setting"/></el-option>
            <el-option value="el-icon-s-grid"><i class="el-icon-s-grid"/></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort" :label-width="formLabelWidth"
                      :rules="[
                                  { required: true, message: '请填写完整'}
                                ]">
          <el-input v-model="form.sort" autocomplete="off"/>
        </el-form-item>
        <el-form-item v-if="ofType!=='add'" label="父ID" prop="parentId" :label-width="formLabelWidth">
          <el-input v-model="form.parentId" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "MenuList",
  components: {

  },
  data(){
    return{
      ofType:'',
      menuTree: [],
      form:{
        id:'',
        name:'',
        url:'',
        icon:'',
        sort:'',
        parentId:''
      },
      dialogFormVisible:false,
      formLabelWidth:'120px'
    }
  },
  created(){
    this.getMenus()
  },
  activated() {
    this.getMenus()
  },
  //离开路由之前执行
  beforeRouteLeave(to,from,next){
    //将下一个页面缓存
    // to.meta.keepAlive = true
    //将当前页面缓存
    // from.meta.keepAlive = true
    next()
  },
  methods:{
    //获取菜单列表
    getMenus(){
      let url = "menu/tree";
      this.$http.get(url).then(res => {
        this.menuTree = res.data.data.items
      })
    },
    //提交表单
    submitForm(formName){
      if(this.form.id){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "menu"
            this.form.title = this.form.name
            this.$http.put(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getMenus()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }else{
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "menu"
            this.form.title = this.form.name
            this.$http.post(url,this.form).then(res=>{
              if(res.data.code===0){
                this.getMenus()
                this.dialogFormVisible = false
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                });
              }
            })
          } else {
            return false;
          }
        });
      }
    },
    //根据ID删除
    deleteById(row) {
      this.$confirm('确定要删除这条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'menu/'+row.id
        this.$http.delete(url).then(res=>{
          if(res.data.code===0){
            this.getMenus()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //打开编辑框
    openFormDialog(row,type){
      this.ofType = type
      this.dialogFormVisible = true;
      //添加菜单
      if(type==='add'){
        this.form = {}
      }
      //添加子菜单
      if(type==='addSub'){
        this.form = {}
        this.form.parentId = row.id
      }
      //编辑菜单
      if(type==='edit'){
        if(!row.parentId){
          this.ofType = 'add'
        }
        this.form = {...row}
      }
    },
    //清空表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
